//
//  YCFriendMatchViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/12.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  添加手机联系人(手机通讯录匹配)

#import "YCFriendMatchViewController.h"

#pragma mark - YCFriendMatchCell

typedef void(^AddFriendButtonClickBlock)();

@interface YCFriendMatchCell : UITableViewCell

@property (readonly ,nonatomic) UIImageView *headerImageView;
@property (readonly ,nonatomic) UILabel *nameLabel;
@property (readonly ,nonatomic) UIButton *addFriendButton;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier addFriendClickBlock:(AddFriendButtonClickBlock)block;
@end

@implementation YCFriendMatchCell
{
    AddFriendButtonClickBlock _addFriendBlock;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier addFriendClickBlock:(AddFriendButtonClickBlock)block
{
    _addFriendBlock = block;
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _headerImageView = [[UIImageView alloc] init];
        _headerImageView.layer.cornerRadius = 60 / 2;
        _headerImageView.layer.masksToBounds  =YES;
        _headerImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        
        _addFriendButton = [[UIButton alloc] init];
        _addFriendButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _addFriendButton.layer.cornerRadius = 20 / 2.0;
        _addFriendButton.layer.masksToBounds  =YES;
        _addFriendButton.backgroundColor = [UIColor yc_colorWithUInt:0x3ba4fa];

        [_addFriendButton setTitle:@"添加好友" forState:UIControlStateNormal];
        [_addFriendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addFriendButton addTarget:self action:@selector(didTouchUpinsideAddFriendButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:self.headerImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.addFriendButton];
        
        [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(15);
            make.width.height.equalTo(@60);
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.headerImageView.mas_right).with.offset(10);
        }];
        
        CGSize size = [[_addFriendButton titleForState:UIControlStateNormal] yc_sizeWithFont:_addFriendButton.titleLabel.font MaxSize:CGSizeMake(MAXFLOAT, 0)];
        [self.addFriendButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(- 15);
            make.height.equalTo(@20);
            make.width.equalTo(@(size.width + 20));
        }];
    }
    return self;
}

- (void)didTouchUpinsideAddFriendButton:(UIButton *)button
{
    YCLog(@"点击了添加好友");
    !_addFriendBlock?:_addFriendBlock();
}

@end

#pragma mark - YCFriendMatchViewController

@interface YCFriendMatchViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (readonly ,nonatomic) UIImageView *noImageView;
@property (readonly ,nonatomic) UILabel *noLabel;
@property (readonly ,nonatomic) UILabel *headerLabel;
@property (readonly ,nonatomic) UITableView *tableView;

@end

@implementation YCFriendMatchViewController

@synthesize noImageView = _noImageView;
@synthesize noLabel = _noLabel;
@synthesize headerLabel = _headerLabel;
@synthesize tableView = _tableView;

- (UILabel *)headerLabel
{
    if (!_headerLabel)
    {
        _headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        _headerLabel.backgroundColor = [UIColor yc_colorWithUInt:0x444444 alpha:0.4];
        _headerLabel.text = @"匹配到0位联系人";
        _headerLabel.textColor = [UIColor whiteColor];
        _headerLabel.font = [UIFont systemFontOfSize:14];
        _headerLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _headerLabel;
}

- (UIImageView *)noImageView
{
    if (!_noImageView)
    {
        _noImageView = [[UIImageView alloc] init];
        _noImageView.contentMode = UIViewContentModeCenter;
        _noImageView.backgroundColor = [UIColor clearColor];
        [_noImageView setImage:[UIImage imageNamed:@"匹配到你想找的人"]];
        _noImageView.hidden = NO;
    }
    
    return _noImageView;
}

//- (UILabel *)noLabel
//{
//    if (!_noLabel)
//    {
//        _noLabel = [[UILabel alloc] init];
//        _noLabel.text = @"没有帮您匹配到想找的人!";
//        _noLabel.textColor = [UIColor grayColor];
//        _noLabel.font = [UIFont systemFontOfSize:14];
//        _noLabel.textAlignment = NSTextAlignmentCenter;
//        _noLabel.hidden = NO;
//    }
//    
//    return _noLabel;
//}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 80;
        _tableView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
        
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.3)];
        _tableView.tableFooterView.backgroundColor = [UIColor yc_colorWithUInt:0xc8c7cc];
    }
    
    return _tableView;
}

#pragma mark - 生命周期

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"手机通讯录匹配", nil);
    [self yc_addBackBarButtonItem];
    
    [self makeView];
}

- (void)makeView
{
    [self.view addSubview:self.headerLabel];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.noImageView];
//    [self.view addSubview:self.noLabel];
    
    [self.headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.top.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.equalTo(self.headerLabel.mas_bottom);
    }];
    
    [self.noImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view);//.offset(- 25);
        make.centerX.equalTo(self.view);
    }];
    
//    [self.noLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.centerY.equalTo(self.view).offset(25);
//    }];
}



#pragma mark - UITableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;//self.nextFriendsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"IDEN";
    
    YCFriendMatchCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell)
    {
        cell = [[YCFriendMatchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden addFriendClickBlock:^{
            YCLog(@"点击的添加好友---tableview中打印的");
        }];
    }
    
    //    GOCustomerUser *user = self.nextFriendsArray[indexPath.row];
    
    [cell.headerImageView yc_setWebImageWithURLString:@"" holderSize:CGSizeMake(60, 60)];
    cell.nameLabel.text = @"张行";//user.cuNickName;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //    GOCustomerUser *user = self.nextFriendsArray[indexPath.row];
    //    GOShowOffModel *model = [[GOShowOffModel alloc] init];
    //    model.userId = user.cuUserId;
    //    model.headPhoto = user.cuHeadPhoto;
    //    model.nickName = user.cuNickName;
    //    GOOstentatiousForOtherViewController *vc = [[GOOstentatiousForOtherViewController alloc] initWithModel:model];
    //    [self.navigationController pushViewController:vc animated:YES];
}

@end
